package com.guoxue.auth;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import com.guoxue.domain.UserVo;
import com.guoxue.util.distributedLock.RedisLock;

public class MuliThread extends Thread{
    MuliThread(String name){
        super(name);//调用父类带参数的构造方法
        
    }
    public void run(){
    	while(true){
	    	String  key="tes5tLock";
	    	RedisLock redisLock=new RedisLock(key);
	    	try {
		    	if(redisLock.lock(60000)){
		            String driver = "com.mysql.jdbc.Driver";  
		            String url = "jdbc:mysql://192.168.0.2:3306/copyguoxue?useUnicode=true&amp;characterEncoding=UTF-8";  
		            String user = "root";  
		            String pwd = "root";  	  
	    	        CommonSql commonSql = new CommonSql(driver, url, user, pwd);  
	    	        if (commonSql.openConnection()) {  
	    	            DatabaseMetaData dbMetaData;
						dbMetaData = commonSql.getConnection().getMetaData(); 
	    	        } else {  
	    	            System.out.println("数据库连接失败！");  
	    	        }  
	    	        UserVo userVo =new UserVo();
	    	        String  sql="select * from uservo u where u.id =?";
	    	        ResultSet rl=commonSql.execQuery(sql, 285);
	    	        while(rl.next()){
	    	        	userVo.setId(rl.getInt("id"));
	    	        	userVo.setStatus(rl.getInt("status")+1);
	    	        }
	    	        boolean re=false;
	    	        if(userVo.getStatus()>=0){
	    	        	 String  sql2="update uservo u set u.status=? where u.id =285";
	    	        	 re=commonSql.execUpdate(sql2, userVo.getStatus());
	    	        	 System.out.println(userVo.getId()+"======"+userVo.getStatus()+"本次是"+this.getName()+":执行+1");
	    	        }else{
	    	        	System.out.println(re+"=====");
	    	        }
	    	        commonSql.closeAll();  
		    		long number = new Random().nextInt(1901) + 100;
					this.sleep(number);	    		
		    	}else{
		    	    System.out.println(this.getName()+":filed to get");
		    	} 
	    	} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				redisLock.unlock();
			}
	    	long number1 = new Random().nextInt(1901) + 100;
			try {
				this.sleep(number1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	    }	
    }
}
